package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
   @Autowired
    private UserDao userDao;
    @Override
    @Transactional(readOnly = true)
    public PageInfo<User> findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<User> users = userDao.findAll();
        return new PageInfo<>(users);
    }

    @Override
    @Transactional(readOnly = true)
    public User findById(int id) {
        //int i=2/0;
        return userDao.selectById(id);
    }

    @Override
    @Transactional
    public int save(User user) {
        return userDao.addUser(user);
    }

    @Override
    @Transactional
    public int update(User user) {
        //int c=2/0;
        return userDao.updateUser(user);
    }

    @Override
    @Transactional
    public int delete(int id) {
        return userDao.deleteById(id);
    }

    @Override
    public User login(String username, String password) {
        return userDao.selectByUsernameAndPassword(username, password);
    }
}
